﻿using System;
using System.Linq;

namespace Sources
{
	/// <summary>
	/// Field Artefact to deploy
	/// </summary>
	public class FieldArtefact : IDeployableObject
	{
		/// <summary>
		/// Constructor which generates an ID automatically
		/// </summary>
		public FieldArtefact()
		{
			ID = Guid.NewGuid();
		}

		/// <summary>
		/// ID for the Field
		/// </summary>
		public Guid ID { get; set; }
		/// <summary>
		/// Name and StaticName of the Field
		/// </summary>
		public string Name { get; set; }
		/// <summary>
		/// Displayname of the Field
		/// </summary>
		public string DisplayName { get; set; }
		/// <summary>
		/// Description of the Field
		/// </summary>
		public string Description { get; set; }
		/// <summary>
		/// Type of the Field
		/// </summary>
		public FieldTypeEnum? Type { get; set; }
		/// <summary>
		/// Group of the Field
		/// </summary>
		public string Group { get; set; }
		/// <summary>
		/// Format for some Fields
		/// </summary>
		public FieldFormatEnum? Format { get; set; }
		/// <summary>
		/// Define the rich text mode
		/// </summary>
		public RichTextModeEnum? RichTextMode { get; set; }
		/// <summary>
		/// Required flag for the Field
		/// </summary>
		public bool? Required { get; set; }
		/// <summary>
		/// Defines if a field is in rich mode
		/// </summary>
		public bool? RichText { get; set; }
		/// <summary>
		/// Define the minimum of a number field
		/// </summary>
		public int? Min { get; set; }
		/// <summary>
		/// Define the maximum of a number field
		/// </summary>
		public int? Max { get; set; }
		/// <summary>
		/// Field to look at
		/// </summary>
		public string FieldRef { get; set; }
		/// <summary>
		/// List where the lookup field looks at
		/// </summary>
		public string ListName { get; set; }
		/// <summary>
		/// Identifies if the Field should be deployed to the RootWeb
		/// </summary>
		public bool ToRootWeb { get; set; }
		/// <summary>
		/// Default value for the Field
		/// </summary>
		public string Default { get; set; }
		/// <summary>
		/// Choices for the Choice Field
		/// </summary>
		public string[] Choices { get; set; }
		/// <summary>
		/// Formula for a calculated field
		/// </summary>
		public string Formula { get; set; }
		/// <summary>
		/// Show this field in new forms
		/// </summary>
		public bool? ShowInNewForm { get; set; }
		/// <summary>
		/// Show this field in display forms
		/// </summary>
		public bool? ShowInDisplayForm { get; set; }
		/// <summary>
		/// Show this field in edit forms
		/// </summary>
		public bool? ShowInEditForm { get; set; }

		/// <summary>
		/// Create a valid xml definition for the Field
		/// </summary>
		/// <returns>The XML for the field to be deployed</returns>
		public string GetXml()
		{
			var xmlString = "<Field";
			
			xmlString += " ID=\"" + ID + "\"";

			if (Name != null) { xmlString += " Name=\"" + Name + "\" StaticName=\"" + Name + "\""; }
			if (DisplayName != null) { xmlString += " DisplayName=\"" + DisplayName + "\""; }
			if (Type != null) { xmlString += " Type=\"" + Type + "\""; }
			if (Group != null) { xmlString += " Group=\"" + Group + "\""; }
			if (Format != null) { xmlString += " Format=\"" + Format + "\""; }
			if (Required != null) { xmlString += " Required=\"" + Required.ToString().ToUpper() + "\""; }
			if (Min != null) { xmlString += " Min=\"" + Min + "\""; }
			if (Max != null) { xmlString += " Max=\"" + Max + "\""; }

			if (RichText != null) { xmlString += " RichText=\"" + RichText.ToString().ToUpper() + "\""; }
			if (RichTextMode != null) { xmlString += " RichTextMode=\"" + RichText + "\""; }

			if (ShowInNewForm != null) { xmlString += " ShowInNewForm=\"" + ShowInNewForm.ToString().ToUpper() + "\""; }
			if (ShowInDisplayForm != null) { xmlString += " ShowInDisplayForm=\"" + ShowInDisplayForm.ToString().ToUpper() + "\""; }
			if (ShowInEditForm != null) { xmlString += " ShowInEditForm=\"" + ShowInEditForm.ToString().ToUpper() + "\""; }
			
			if (Type == FieldTypeEnum.LookupMulti ||
			    Type == FieldTypeEnum.UserMulti)
			{
				{ xmlString += " Mult=\"TRUE\""; }
			}

			xmlString += ">";

			if (Formula != null) { xmlString += "<Formula>" + Formula + "</Formula>"; }
			if (Default != null) { xmlString += "<Default>" + Default + "</Default>"; }
			else if (Type == FieldTypeEnum.Choice &&
			         Choices != null &&
			         Choices.Any())
			{
				xmlString += "<Default>" + Choices.First() + "</Default>";
			}

			if (Choices != null)
			{
				var choicesXml = string.Empty;
				Array.ForEach(Choices, c => choicesXml += $"<CHOICE>{c}</CHOICE>");
				xmlString += "<CHOICES>" + choicesXml + "</CHOICES>";
			}

			

			xmlString += "</Field>";

			return xmlString;
		}
	}
}
